// targets.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [
      { alphabet: 'A', datas: ['ss', 'sssare here'] },
      { alphabet: 'B', datas: ['bbsome', 'bebntries', 'bare here'] },
      { alphabet: 'C', datas: ['csome', 'centries', 'care here'] },
      { alphabet: 'D', datas: ['dsome', 'dentries', 'dare here'] },
      { alphabet: 'E', datas: ['esome', 'eentries', 'eare here'] },
      { alphabet: 'F', datas: ['fsome', 'fentries', 'are here'] },
      { alphabet: 'G', datas: ['gsome', 'gentries', 'gare here'] },
      { alphabet: 'H', datas: ['hsome', 'hentries', 'hare here'] },
      { alphabet: 'I', datas: ['isome', 'ientries', 'iare here'] },
      { alphabet: 'J', datas: ['jsome', 'jentries', 'jare here'] },
      { alphabet: 'K', datas: ['ksome', 'kentries', 'kare here'] },
      { alphabet: 'L', datas: ['lsome', 'lentries', 'lare here'] },
      { alphabet: 'M', datas: ['msome', 'mentries', 'mare here'] },
      { alphabet: 'N', datas: ['nsome', 'nentries', 'nare here'] },
      { alphabet: 'O', datas: ['osome', 'oentries', 'oare here'] },
      { alphabet: 'P', datas: ['psome', 'pentries', 'pare here'] },
      { alphabet: 'Q', datas: ['qsome', 'qentries', 'qare here'] },
      { alphabet: 'R', datas: ['rsome', 'rentries', 'rare here'] },
      { alphabet: 'S', datas: ['some', 'sentries', 'sare here'] },
      { alphabet: 'T', datas: ['tsome', 'tentries', 'tare here'] },
      { alphabet: 'U', datas: ['usome', 'uentries', 'uare here'] },
      { alphabet: 'V', datas: ['vsome', 'ventries', 'vare here'] },
      { alphabet: 'W', datas: ['wsome', 'wentries', 'ware here'] },
      { alphabet: 'X', datas: ['xsome', 'xentries', 'xare here'] },
      { alphabet: 'Y', datas: ['ysome', 'yentries', 'yare here'] },
      { alphabet: 'Z', datas: ['zsome', 'zentries', 'zare here'] },
    ],
    alpha: '',
    windowHeight: '',
    inputShowed: false,
    inputVal: "",
  },

  showInput: function () {
    this.setData({
      inputShowed: true
    });
  },
  hideInput: function () {
    try {
      if (this.data.inputVal.length > 0) {
        this.setData({
          inputShowed: false
        })
      }
      this.setData({
        inputVal: "",
      });
    } catch (err) { }

  },
  clearInput: function () {
    this.setData({
      inputVal: ""
    });
  },
  inputTyping: function (e) {
    try {
      var key = e.detail.value, i, k;
      key = key.toUpperCase();
      var list = this.data.cityList,
        cityArr = new Array();
      if (list == null || typeof (list) == 'undefined') {
        return;
      }
      for (i = 0; i < list.length; i++) {
        for (k = 0; k < list[i].AlphabetData.length; k++) {
          if (list[i].AlphabetData[k].Alphabet.indexOf(key) == 0 || list[i].AlphabetData[k].Name.indexOf(key) == 0) {
            cityArr.push(list[i].AlphabetData[k])
          }
        }
      }
      this.setData({
        isSearch: cityArr,
        inputVal: e.detail.value,
      })
    } catch (e) { }
  },

  handlerAlphaTap(e) {
    let { ap } = e.target.dataset;
    this.setData({ alpha: ap });
  },
  handlerMove(e) {
    let { list } = this.data;
    let moveY = e.touches[0].clientY;
    let rY = moveY - this.offsetTop;
    if (rY >= 0) {
      let index = Math.ceil((rY - this.apHeight) / this.apHeight);
      if (0 <= index < list.length) {
        let nonwAp = list[index];
        nonwAp && this.setData({ alpha: nonwAp.alphabet });
      }
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 测试
    try {
      app.showLoadings();
      this.stationLine();
      var res = wx.getSystemInfoSync();
      this.pixelRatio = res.pixelRatio;
      this.apHeight = 16;
      this.offsetTop = 80;
      this.setData({ windowHeight: res.windowHeight });
    } catch (e) { }
  },

  stationLine: function () {
    try {
      let getToken = app.accesstoken ? app.accesstoken : app.stanToken;
      var that = this;
      wx.request({
        url: app.serviceurl + 'StationLine/QueryDestination',
        header: {
          Authorization: "Bearer " + getToken
        },
        data: { StartStaionId: app.globalcode },
        dataType: 'json',
        success: function (res) {
          if (!res.data.Result) {
            that.setData({
              Result: false,
              message: res.data.Message
            })
            return
          };

          that.setData({
            cityList: res.data.Alphabet,
            Result: true
          });
        },
        fail: function () {
          console.log("调用失败");
          that.setData({ Result: false })
        },
        complete: function () {
          app.hideLoadings();
          that.setData({ placeholder: '中文/简拼' });
        }
      })
    } catch (err) { }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    if (res.from === 'menu') {
      return app.OnShare();
    }
  },

  selecttarget: function (event) {
    try {
      let target = event.currentTarget.dataset.target;
      app.target = target;
      wx.setStorage({
        key: 'target',
        data: target,
      });
      wx.navigateBack({});
    } catch (err) { }
  }
})